package triangle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;

public class TriangleChecker {

	public String checkTriangle(double a, double b, double c) {
		String ret = "";
		final int ONE_MILLION = 1000000;

		//vaidate input
		if(a <= 0 || b <= 0 || c <=0) {
			ret = "Error: Invalid input value. A triangle's sides must each be greater than zero.";
		} else if(a > ONE_MILLION || b > ONE_MILLION || c > ONE_MILLION) {
			ret = "Error: Invalid input value. Sides must each be less than one million.";
		}
		else {
			//each number is valid; check if together they are a valid triangle
			//to be valid: the sum of any two sides must be greater than the third side
			if(a+b <= c || a+c <= b || b+c <= a) {
				ret = "Error: Invalid measurements. Cannot form a triangle.";
			}
			else {
				//we're good! Initiate classification...
				ret = "";

				//check for right triangle, using pythagorean theorem
				ArrayList<Double> list = new ArrayList<Double>();
				list.add(a);
				list.add(b);
				list.add(c);
				Collections.sort(list);

				//a^2 + b^2 should equal c^2
				double aSquared = Math.pow(list.get(0), 2);
				double bSquared = Math.pow(list.get(1), 2);
				double cSquared = Math.pow(list.get(2), 2);

				//allow for 1% margin of error
				if(aSquared + bSquared >= cSquared - (.01 * cSquared)
						&& aSquared + bSquared <= cSquared + (.01 * cSquared)){
					ret += "Right ";
				}
				
				//check triangle type
				if(a == b && a == c) {
					ret += "Equilateral";
				}
				else if(a == b || a == c || b == c) {
					ret += "Isosceles";
				}
				else {
					ret += "Scalene";
				}
				
				ret += " Triangle";
			}
		}

		return ret;
	}

	public static void main(String[] args) {
		Scanner console = new Scanner(System.in);

		System.out.print("Enter the length of the first side of the triangle: ");
		try {
			double a = console.nextDouble();

			System.out.print("Enter the length of the second side of the triangle: ");
			double b = console.nextDouble();

			System.out.print("Enter the length of the third side of the triangle: ");
			double c = console.nextDouble();		

			System.out.println();

			TriangleChecker checker = new TriangleChecker();
			System.out.println(checker.checkTriangle(a, b, c));		
		}
		catch(InputMismatchException e) {
			System.out.println("\nError: invalid non-numeric input.");
		}

		System.out.print("\n\nPress enter to quit... ");
		String s = console.nextLine();
		s = console.nextLine();
	}
}
